package com.njsf.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
<<<<<<< HEAD
=======
import java.util.HashSet;
import java.util.Set;
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8

@WebFilter(filterName = "LoginFilter",urlPatterns = "/back/*")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {

<<<<<<< HEAD
        String excludeUrl = "/back/checklogin | /back/login";
=======
        Set<String> excludeUrl = new HashSet<>();
        excludeUrl.add("/back/checklogin");
        excludeUrl.add("/back/login");
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String contextPath = request.getContextPath();
        String servletPath = request.getServletPath();

<<<<<<< HEAD
        Object admin = request.getSession().getAttribute("admin");
        if (admin == null && !excludeUrl.contains(servletPath)){
            response.sendRedirect(contextPath+"/back/login");
=======
        System.out.println(contextPath+"---"+servletPath);

        Object admin = request.getSession().getAttribute("admin");
        if (admin == null && !excludeUrl.contains(servletPath)){
            response.sendRedirect(contextPath);
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
        }else{
            /*过滤器放行请求*/
            filterChain.doFilter(request,response);
        }
    }

    @Override
    public void destroy() {}
}
